package com.feihong.txgw.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.feihong.txgw.R;
import com.feihong.txgw.adapter.Holder.HotCityViewAdapter;
import com.feihong.txgw.inter.listener.location_city.HotCityListener;
import com.feihong.txgw.model.bean.user.CityInfo;
import com.feihong.txgw.model.bean.user.SortModel;
import com.feihong.txgw.utils.UserLoginInfo;

/**
 * 地理位置信息列表所需要的adapter
 * Created by PVer on 2017/6/30.
 */
public class LocationSortAdapter extends BaseAdapter {

    private SortModel.DataBean aBeenlist;
    private Context mContext;
    private static final int HOTCITY_TYPE = 1;
    private static final int CITY_TYPE = 2;


    public LocationSortAdapter(Context mContext) {
        this.mContext = mContext;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     *
     * @param list
     */
    public void updateListView(SortModel.DataBean list) {
        this.aBeenlist = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowHotCity) {
            if (position == 0) {
                return HOTCITY_TYPE;
            } else {
                return CITY_TYPE;
            }
        } else {
            return CITY_TYPE;
        }

    }

    @Override
    public int getCount() {
        if (isShowHotCity) {
            if (aBeenlist == null || aBeenlist.getCity() == null) {
                return 1;
            }
            return this.aBeenlist.getCity().size() + 1;
        } else {
            if (aBeenlist == null || aBeenlist.getCity() == null) {
                return 1;
            }
            return aBeenlist.getCity().size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (isShowHotCity) {
            if (position > 0) {
                return aBeenlist.getCity().get(position - 1);
            } else {
                return null;
            }
        } else {
            return aBeenlist.getCity().get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup arg2) {

        if (getItemViewType(position) == HOTCITY_TYPE) {
            HotCityViewHolder viewHolder = new HotCityViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.location_hot_city_layout, arg2, false);
            viewHolder.initView(view);
            viewHolder.initData(position);
            return view;
        } else {
            ViewHolder viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.localitem, arg2, false);
            viewHolder.intView(view);
            if (isShowHotCity) {
                viewHolder.initData(position - 1);
            } else {
                viewHolder.initData(position);
            }
            return view;
        }
    }


    final class ViewHolder {
        TextView tvLetter;
        TextView tvTitle;
        TextView longitude;
        TextView latitude;
        TextView citycode;
        LinearLayout showCityInfoLayout;

        void intView(View convertView) {
            tvTitle = (TextView) convertView.findViewById(R.id.title);
            tvLetter = (TextView) convertView.findViewById(R.id.catalog);
            showCityInfoLayout = (LinearLayout) convertView.findViewById(R.id.showCityInfoLayout);
        }

        void initData(int position) {

            if(aBeenlist == null || aBeenlist.getCity() == null){
                showCityInfoLayout.setVisibility(View.GONE);
                return;
            }
            SortModel.DataBean.CityBean cityBean = aBeenlist.getCity().get(position);
            showCityInfoLayout.setVisibility(View.VISIBLE);
            tvTitle.setText(cityBean.getRegion_name());
            int section = getSectionForPosition(position);
            if (position == getPositionForSection(section)) {
                tvLetter.setVisibility(View.VISIBLE);
                tvLetter.setText(cityBean.getFirst_key());
            } else {
                tvLetter.setVisibility(View.GONE);
            }
        }
    }


    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        return aBeenlist.getCity().get(position).getFirst_key().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        if (aBeenlist != null && aBeenlist.getCity() != null) {
            for (int i = 0; i < aBeenlist.getCity().size(); i++) {
                String sortStr = aBeenlist.getCity().get(i).getFirst_key();
                char firstChar = sortStr.toUpperCase().charAt(0);
                if (firstChar == section) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * 提取英文的首字母，非英文字母用#代替。
     *
     * @param str
     * @return
     */
    private String getAlpha(String str) {
        String sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    class HotCityViewHolder {

        private GridView mHotCity;
        HotCityViewAdapter mHotCityAdapter;

        public HotCityViewHolder() {
            mHotCityAdapter = new HotCityViewAdapter(mContext);
        }

        void initView(View convertView) {
            mHotCity = (GridView) convertView.findViewById(R.id.location_hot_city_gv);
        }

        void initData(int position) {
            mHotCity.setAdapter(mHotCityAdapter);
            mHotCityAdapter.setData(aBeenlist.getHot_city());
            mHotCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    HotCityViewAdapter adapter = (HotCityViewAdapter) parent.getAdapter();
                    SortModel.DataBean.HotCityBean bean = (SortModel.DataBean.HotCityBean) adapter.getItem(position);

                    CityInfo cityinfo = new CityInfo();
                    cityinfo.setLatitude(bean.getLatitude());
                    cityinfo.setLongitude(bean.getLongitude());
                    cityinfo.setCityCode(bean.getAreacode());
                    cityinfo.setCityName(bean.getRegion_name());
                    UserLoginInfo.setUserCityInfo(cityinfo);
//                    Intent intent = new Intent(mContext, HomeActivity.class);
//                    intent.putExtra("city", cityinfo.getCityName());
//                    intent.putExtra("Longitude", cityinfo.getLongitude());
//                    intent.putExtra("Latitude", cityinfo.getLatitude());
//                    intent.putExtra("CityCode", cityinfo.getCityCode());
//                    mContext.startActivity(intent);

                    mHotCityListener.setHotCityListtener(cityinfo);
                }
            });
        }
    }

    HotCityListener mHotCityListener;

    public void setHotCityListener(HotCityListener hotCityListener) {
        mHotCityListener = hotCityListener;
    }

    private boolean isShowHotCity = true;

    public void setShowHotCity(boolean hotCity) {
        isShowHotCity = hotCity;
        notifyDataSetChanged();
    }
}
